/*
 * @Description: 通讯录store
 * @Author: wws1993
 * @Date: 2020-12-02 10:53:21
 * @LastEditTime: 2020-12-03 15:50:32
 * @LastEditors: wws1993
 * @FilePath: \todoManager\src\components\MailListModule\modal.ts
 */

import lib from '@/lib';
import MailModal from '@/service/MailModal';

export default {
  namespaced: true,
  state: {
    visible: false,                                               // 通讯录是否展示
    addState: false,                                              // 添加联系人到通讯录弹窗参数
    data: [],                                                     // 联系人数据
    form: {},                                                     // 联系人表单
  } as MailListModal.Instance,
  mutations: {
    toggleVisible:  (state: any) => state.visible = !state.visible,
    toggleAdd: (state: any) => state.addState = !state.addState,
  },
  actions: {
    query({state}: MailListModal.ActionsFirPayload) {
      MailModal.request().then((res: MailListModal.Iuser[] | null) => {
        state.data = res || [];
      });
    },
    add({state, dispatch, commit}: MailListModal.ActionsFirPayload) {
      MailModal.save(state.form).then(() => {
        state.form = {};
        state.addState = false;
        dispatch('query');
        lib.notify('保存成功', '', 'success');
      });
    },
  },
};
